package top.yehot.cmmu.mybatis;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;

import lombok.AllArgsConstructor;
import top.yehot.cmmu.mybatis.model.MybatisProperties;

/**
 * MybatisPlus配置
 * 
 * @author lquan
 * @date 2022-9-10 11:46:07
 */
@Configuration
@AllArgsConstructor
@EnableConfigurationProperties(MybatisProperties.class)
public class MybatisPlusAutoConfig {

	private final MybatisProperties properties;

	/**
	 * 配置mybatis plus 插件
	 *
	 * @return mybatis plus 插件
	 */
	@Bean
	public MybatisPlusInterceptor MybatisPlusInterceptor() {
		MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
		if (properties.getPagination().isEnabled()) {
			PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
			paginationInnerInterceptor.setMaxLimit(properties.getPagination().getLimit());
			paginationInnerInterceptor.setOverflow(properties.getPagination().isOverflow());

			interceptor.addInnerInterceptor(paginationInnerInterceptor);
		}
		if (properties.getOptimisticLocker().isEnabled()) {
			interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
		}
		if (properties.getBlockAttack().isEnabled()) {
		    interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
		}

		return interceptor;
	}

}
